fn main() {
    
    // Iter::map 操作可能失败
    // let strings = vec!["tofu", "93", "18"];
    // let numbers: Vec<_> = strings.into_iter().map(|s| s.parse::<i32>()).collect();
    // println!("Results: {:?}", numbers);
    
    // 使用 filter_map() 忽略失败的项
    let strings1 = vec!["tofu", "93", "18"];
    let numbers1: Vec<_> = strings1
        .into_iter()
        .filter_map(|s| s.parse::<i32>().ok())
        .collect();
    println!("Results1: {:?}", numbers1);

    // 使用 collect() 使整个操作失败
    let strings2 = vec!["tofu", "93", "18"];
    let numbers2: Result<Vec<_>, _> = strings2
        .into_iter()
        .map(|s| s.parse::<i32>())
        .collect();
    println!("Results2: {:?}", numbers2);

    // 使用 Partition() 收集所有合法的值与错误
    let strings3 = vec!["tofu", "93", "18"];
    let (numbers3, errors): (Vec<_>, Vec<_>) = strings3
        .into_iter()
        .map(|s| s.parse::<i32>())
        .partition(Result::is_ok);
    let numbers3: Vec<_> = numbers3.into_iter().map(Result::unwrap).collect();
    let errors3: Vec<_> = errors.into_iter().map(Result::unwrap_err).collect();
    println!("Numbers3: {:?}", numbers3);
    println!("Errors3: {:?}", errors3);
}
